import {api_url} from '@/static/js/config';
const mixin = {
  data(){
      return{
          uploadType: null,
      }
  },
  methods:{
    chooseImg(type){
      this.uploadType = type;
      uni.chooseImage({
        count: 1, 
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album'], //从相册选择
        success: res => {
          console.log('chooseImg', res);
          if(res.tempFiles[0].size > 20 * 1024 * 1024){
            uni.showToast({title: '图片大小不能超过20M'})
          }else{
            this.handleUpload(res.tempFiles[0].path);
          }
        }
      });
    },
    handleUpload(url){
      let arr = url.split('/');
      let fileName = arr[arr.length - 1];
        uni.showLoading({title: '上传中...'});
        uni.uploadFile({
            url: api_url + '/admin/file/upload',
            filePath: url,
            name: 'file',
            header: {
                // 'content-type': 'multipart/form-data',
                'Authorization': this.$store.state.access_token,
                // 'Source-Type': 2
                // "sn": this.$utils.encryp(fileName)
            },
            formData: { resType: 'file' },
            success: res => {
                let {code, data} = JSON.parse(res.data);
                if(code == 0 || code == 200){
                    this.uploadSuccess(data)                   
                }else{
                    uni.showToast({title: '上传失败！'});
                }
            },
            fail: err => {
                uni.showToast({title: '上传失败！'});
            },
            complete: ()=>{
                uni.hideLoading()
            }
        });                
    },
    uploadSuccess(data){

    }
  }
}

export default mixin